
//#include "lexer.hpp"

#include <iostream>
#include <map>
#include <cstdlib>
#include <string>
#include <vector>

//#include "symbolTable.hpp"

#include "symbolTable.hpp"

//#include "lexerv1.hpp"

//std::vector<std::vector<std::vector<string>>> cube;


//using namespace std;

//string cube[6][7][7];
//std::vector<string> stack;
std::map<string, std::vector<std::pair<string,string>>> symbolTable;
std::vector<param> parameters;

//get functions
std::map<string, std::vector<std::pair<string,string>>> * getST()
{
	return &symbolTable;
}

std::vector<param> * getParameters()
{
	return &parameters;
}

string getType(string id, string scope)
{
	std::map<string, std::vector<std::pair<string,string>>>::iterator temp;
	string type = "";

	temp = getsymbol(id);
	if(temp == symbolTable.end())
	{
		//identifier doesn't exist in symbol table
	}
	else
	{
		//creating a temporary variable for all the scopes attached to that particular identifier
		std::vector<std::pair<string,string>> allscopes = temp->second;
		
		//iterates through the vector and checks each pair's scope for the passed scope
		for(int i = 0; i < allscopes.size(); i++)
		{
			//if the passed scope & type equals the pair's saved scope & type, then identifier exists in scope
			if(scope.compare(allscopes[i].second) == 0)
				type = allscopes[i].first;
		}
		
		//otherwise the identifier doesn't already exist in the passed scope
	}
	return type;

}




void insert(string id, string type, string scope)
{
	//creating the type/scope pair
	std::pair<string, string> tmp;
	tmp.first = type;
	tmp.second = scope;

	std::vector<std::pair<string,string>> temp;

	if(symbolTable.find(id) == symbolTable.end())
	{
		//adding the type/scope pair to a vector
		temp.push_back(tmp);

		//adding the new id with new vector
		symbolTable[id] = temp;
	}
	else //id already exists in symbol table
	{
		(symbolTable.find(id)->second).push_back(tmp);
	}
}

std::map<string, std::vector<std::pair<string,string>>>::iterator getsymbol(string id)
{
	std::map<string, std::vector<std::pair<string,string>>>::iterator temp;
	temp = symbolTable.find(id);
	return temp;
}

bool search(string id, string type, string scope)
{
	std::map<string, std::vector<std::pair<string,string>>>::iterator temp;
	temp = symbolTable.find(id);

	if(temp == symbolTable.end())
	{
		//identifier doesn't exist in symbol table
		return false;
	}
	else
	{
		//creating a temporary variable for all the scopes attached to that particular identifier
		std::vector<std::pair<string,string>> allscopes = temp->second;
		
		//iterates through the vector and checks each pair's scope for the passed scope
		for(int i = 0; i < allscopes.size(); i++)
		{
			//if the passed scope & type equals the pair's saved scope & type, then identifier exists in scope
			if(scope.compare(allscopes[i].second) == 0 && type.compare(allscopes[i].first) == 0)
				return true;
		}
		
		//otherwise the identifier doesn't already exist in the passed scope
		return false;
	}
}

bool search(string id, string scope)
{
	std::map<string, std::vector<std::pair<string,string>>>::iterator temp;
	temp = symbolTable.find(id);

	if(temp == symbolTable.end())
	{
		//identifier doesn't exist in symbol table
		return false;
	}
	else
	{
		//creating a temporary variable for all the scopes attached to that particular identifier
		std::vector<std::pair<string,string>> allscopes = temp->second;
		
		//iterates through the vector and checks each pair's scope for the passed scope
		for(int i = 0; i < allscopes.size(); i++)
		{
			//if the passed scope & type equals the pair's saved scope & type, then identifier exists in scope
			if(scope.compare(allscopes[i].second) == 0)
				return true;
		}
		
		//otherwise the identifier doesn't already exist in the passed scope
		return false;
	}


}

void print()
{
	std::vector<std::pair<string,string>> temp;
	for(std::map<string, std::vector <std::pair<string,string> > >::const_iterator it = symbolTable.begin(); it != symbolTable.end(); ++it)
	{
		std::cout << it->first << " ";
		temp = it->second;
		for(int j=0; j < temp.size(); j++)
		{
			std::cout<<"\t"<<temp[j].first<<" "<<temp[j].second<<std::endl;
		}
	}
}
